Crispo - Excel Challenge 13 2025

excel-challenges
weekly-exercises
Easy Sunday Excel Challenge
Published

March 30, 2025

Illustration for Crispo - Excel Challenge 13 2025

Challenge Description

Easy Sunday Excel Challenge

⭐ Problem Solution Staff No. Date Time Time In

Solutions

library(tidyverse)
library(readxl)

path = "files/Challenge1325.xlsx"
input = read_excel(path, range = "B3:D19")
test  = read_excel(path, range = "F3:I17") %>%
  mutate(across(starts_with("Time"), ~hms::as_hms(.))) 

expanded = expand.grid(unique(input$`Staff No.`), unique(input$Date)) %>%
  left_join(input, by = c("Var1" = "Staff No.", "Var2" = "Date")) %>%
  mutate(Time = hms::as_hms(Time)) %>%
  arrange(Var1, Var2, Time) %>% 
  mutate(n = n(),
         rn = row_number(),
         .by = c("Var1", "Var2")) %>%
  filter(rn == 1|rn == n) %>%
  mutate(rn = row_number(), .by = c(Var1, Var2)) %>%
  select(-n) %>%
  pivot_wider(names_from = rn, values_from = Time) %>%
  arrange(Var2, Var1)

colnames(expanded) = colnames(test)
print(expanded)
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data to the grain required by the task

    • Builds the intermediate helper columns that drive the final answer

  • Strengths:

    • The R solution stays compact and mirrors the workbook logic closely.
  • Areas for Improvement:

    • The code assumes the workbook layout and named ranges remain stable.
  • Gem:

    • The best part of the solution is choosing a tidy intermediate shape before producing the final answer.
import pandas as pd
from itertools import product
from datetime import time

path = "files/Challenge1325.xlsx"
input = pd.read_excel(path, sheet_name=0, usecols="B:D", skiprows=2, nrows=17)
test = pd.read_excel(path, sheet_name=0, usecols="F:I", skiprows=2, nrows=14).rename(columns=lambda x: x.replace('.1', ''))

df = pd.DataFrame(product(input['Staff No.'].unique(), input['Date'].unique()), columns=['Staff No.', 'Date'])
df = df.merge(input, on=['Staff No.', 'Date'], how='left')
df = df.sort_values(by=['Staff No.', 'Date', "Time"]).reset_index(drop=True)
df = df.groupby(['Staff No.', 'Date']).apply(lambda group: group.iloc[[0, -1]]).reset_index(drop=True)
df = df.drop_duplicates().reset_index(drop=True)
df['Count'] = df.groupby(['Staff No.', 'Date']).cumcount() + 1
df_pivot = df.pivot(index=['Staff No.', 'Date'], columns='Count', values='Time').reset_index()
df_pivot = df_pivot.sort_values(by=['Date', 'Staff No.']).reset_index(drop=True)
df_pivot.columns = ['Staff No.', 'Date', 'Time In', 'Time Out']

print(df_pivot)
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data to the grain required by the task

    • Aggregates or ranks values at the correct grouping level

  • Strengths:

    • The Python version keeps the same rule in a direct pandas-oriented workflow.
  • Areas for Improvement:

    • As with the R version, any workbook layout change would require small adjustments.
  • Gem:

    • The implementation stays close to the stated challenge instead of adding unnecessary complexity.

Difficulty Level

This task is moderate:

  • It combines familiar Excel-style logic with at least one non-trivial reshape, grouping, or parsing step.

  • The answer depends on getting the output layout exactly right.